<template>
  <div class="my-edit-box">
    <van-nav-bar title="编辑资料" left-arrow @click-left="toMy" />
    <div class="img-box">
      <van-image round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示区 -->
    <van-cell-group>
      <van-cell
        title="名称"
        is-link
        :value="userInfo.name"
        @click="showNameDialog"
      />
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender == 0 ? '男' : '女'"
        @click="showGenderDialog"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="showBirthday"
      />
    </van-cell-group>
    <!-- 编辑昵称弹出框 -->
    <van-dialog
      @confirm="saveNanme"
      v-model="isNameShow"
      title="修改昵称"
      show-cancel-button
    >
      <!-- ref绑在输入框里 -->
      <van-field ref="nameField" v-model="name" />
    </van-dialog>
    <!-- 性别弹出层 -->
    <van-popup v-model="isGenderShow" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="isGenderShow = false"
      />
      <van-cell title="男" is-link @click="changeGender(0)" />
      <van-cell title="女" is-link @click="changeGender(1)" />
    </van-popup>
    <!-- 生日的弹出层 -->
    <van-popup v-model="isBirthdayShow" position="bottom">
      <van-datetime-picker
        v-model="birthday"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="isBirthdayShow = false"
        @confirm="saveBirthday"
      />
    </van-popup>
    <div class="cropper-box" v-show="isShowCropper">
      <vueCropper
        ref="cropper"
        fixed
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
        :img="img"
      ></vueCropper>
      <van-button @click="submitPhoto" class="confirm-btn" type="primary"
        >确认</van-button
      >
      <van-button @click="isShowCropper = false" class="cancel-btn" type="info"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo, uploadPhoto } from '../../api/edit'
import moment from 'moment'
// 导入裁切组件
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  data () {
    return {
      name: '',
      editIcon: false,
      isNameShow: false,
      isGenderShow: false,
      isBirthdayShow: false,
      birthday: new Date(2021, 9, 1),
      minDate: new Date(2021, 0, 1),
      maxDate: new Date(2025, 10, 1),
      img: '', // 裁切的图片
      isShowCropper: false // 是否显示裁切区域
    }
  },
  methods: {
    toMy () {
      this.$router.push({ path: '/my' })
    },
    showNameDialog () {
      this.isNameShow = true
      this.name = this.userInfo.name
      this.$nextTick(() => {
        // console.log(this.$refs.nameField)
        this.$refs.nameField.focus()
      })
    },
    saveNanme () {
      editUserInfo({ name: this.name }).then(res => {
        this.$toast.success('更新成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: res.data.data.name
        })
        this.isGenderShow = false
      })
    },
    showGenderDialog () {
      this.isGenderShow = true
    },
    changeGender (gender) {
      editUserInfo({ gender })
        .then(res => {
          // console.log(res)
          this.$toast.success('性别更新成功')
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            gender
          })
        })
        .catch(err => {
          console.log(err)
        })
    },
    showBirthday () {
      this.isBirthdayShow = true
      this.birthday = new Date(this.userInfo.birthday)
    },
    saveBirthday () {
      // console.log('saveBirthday')
      const birthday = moment(this.birthday).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        // console.log(res)
        this.$toast.success('生日更新成功')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: res.data.data.birthday
        })
        this.isBirthdayShow = false
      })
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      this.isShowCropper = true
      // console.log(file)
      this.img = file.content
    },
    submitPhoto () {
      this.$refs.cropper.getCropBlob(blob => {
        // do something
        const data = new FormData()
        data.append('photo', blob)
        this.$toast.loading('头像上传中...')
        uploadPhoto(data).then(res => {
          this.$toast.success('头像更新成功')
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.isShowCropper = false
        })
      })
    }
  },
  computed: mapState(['userInfo']),
  components: {
    VueCropper
  }
}
</script>

<style lang="less">
.my-edit-box {
  .van-nav-bar {
    background-color: #3789f4;

    .van-nav-bar__left i {
      color: #fff;
    }
  }
  .img-box {
    position: relative;
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
    .van-uploader {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      width: 120px;
      height: 120px;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
      }
    }
  }
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    // 不能使用这个z-index
    // z-index: 999;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>
